package com.yu.yurpcspringbootstarter.bootstrap;

import com.yu.rpc.core.RpcApplication;
import com.yu.rpc.core.config.RpcConfig;
import com.yu.rpc.core.server.tcp.VertxTcpServer;
import com.yu.yurpcspringbootstarter.annotation.EnableRpc;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.context.annotation.ImportBeanDefinitionRegistrar;
import org.springframework.core.type.AnnotationMetadata;

public class RpcInitBootStrap implements ImportBeanDefinitionRegistrar {
    public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) {
        RpcApplication.init();

        Boolean needServer = (Boolean) importingClassMetadata.getAnnotationAttributes(EnableRpc.class.getName())
                .getOrDefault("needServer", false);
        final RpcConfig rpcConfig = RpcApplication.getConfig();
        if (needServer) {
            VertxTcpServer vertxTcpServer = new VertxTcpServer();
            vertxTcpServer.doStart(rpcConfig.getServerPort());
        }
    }
}
